package com.eques.view;


import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.eques.bean.WifiListBean;
import com.eques.demo.anim.AnimalActivity;
import com.eques.demo.bind.BindDevActivity;
import com.eques.demo.R;
import com.eques.demo.adapter.WifiListAdapter;
import com.eques.demo.anim.AnimalClockActivity;
import com.eques.util.ScreenUtils;

import java.lang.ref.WeakReference;
import java.util.List;


/**
 * @Package: com.eques.doorbell.tools
 * @ClassName: PopUtils
 * @Description: Pop
 * @Author: hgz
 * @CreateDate: 2020/10/26 11:42
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PopUtils {
    private static final String TAG = "PopUtils";

    public static final int SEND_FOOD = 1; //宠物喂食
    public static final int WIFI_LIST_ANIMAL = 2;//宠物绑定WIFI列表

    private Activity activity;

    private boolean isOutsideTouchable = false;
    // 云存储套餐适配器

    private List<WifiListBean> wifilistBeans;
    private PopupWindow popWindow;
    private int sendFoodType = 0;
    private WeakReference<View> viewWeakReference = null;

    public PopUtils setView(View parentView) {
        this.viewWeakReference = new WeakReference<>(parentView);
        return this;
    }

    public PopUtils setWifiList(List<WifiListBean> wifilistBeans) {
        this.wifilistBeans = wifilistBeans;
        return this;
    }


    private int chooseIndex=0;
    public PopUtils setChooseIndex(int chooseIndex) {
        this.chooseIndex = chooseIndex;
        return this;
    }


    public PopUtils(Activity activity) {
        this.activity = activity;
    }
    private int openType=-1;
    /**
     * 展示pop
     *
     * @param openType
     */
    public void initShowPop(int openType) {
        View viewLayout = null;
        this.openType=openType;
        switch (openType) {
            case SEND_FOOD:
                viewLayout = selectFoodNum();
                break;
            case WIFI_LIST_ANIMAL:
                viewLayout = wifiListLayout();
                break;
            default:
                break;
        }

        if (popWindow == null) {
            if ( openType == WIFI_LIST_ANIMAL
            || openType ==SEND_FOOD) {
                popWindow = new PopupWindow(viewLayout,
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            } else {
                int width = (ScreenUtils.getScreenWidth(activity) * 3) / 4;
                popWindow = new PopupWindow(viewLayout,
                        width, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            // 设置SelectPicPopupWindow弹出窗体可点击
            popWindow.setFocusable(isOutsideTouchable);
            popWindow.setOutsideTouchable(isOutsideTouchable);
            popWindow.setBackgroundDrawable(new BitmapDrawable());
        }
        if (popWindow.isShowing()) {
            popWindow.dismiss();
        }
        ScreenUtils.setBackgroundAlpha(activity, 0.5f);
        try {

                popWindow.showAtLocation(viewWeakReference.get(), Gravity.BOTTOM, 0, 0);
            popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    dismissPop();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 是否可点击外围取消Pop
     *
     * @param isOutsideTouchable
     * @return
     */
    public PopUtils setOutsideTouchable(boolean isOutsideTouchable) {
        this.isOutsideTouchable = isOutsideTouchable;
        return this;
    }
    public PopUtils setFoodType(int foodType) {
        this.sendFoodType = foodType;
        return this;
    }


    int value = 1;

    public View selectFoodNum() {
        View viewLayout = activity.getLayoutInflater().inflate(R.layout.item_select_food_layout, null);
        NumberPicker numberpicker = viewLayout.findViewById(R.id.numberpicker);
        Button button = viewLayout.findViewById(R.id.btn_send_food);
        TextView tvSendFoodCancel = viewLayout.findViewById(R.id.tv_send_food_cancel);
        TextView tvSendFoodSave = viewLayout.findViewById(R.id.tv_send_food_save);
        if (sendFoodType == 1) {
            tvSendFoodCancel.setVisibility(View.GONE);
            tvSendFoodSave.setVisibility(View.GONE);
        } else {
            tvSendFoodCancel.setVisibility(View.VISIBLE);
            tvSendFoodSave.setVisibility(View.VISIBLE);
            button.setVisibility(View.GONE);
        }
        value=1;
        numberpicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                value = newVal;
            }
        });
        tvSendFoodSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((AnimalClockActivity) activity).sendFood(value);
                dismissPop();
            }
        });
        tvSendFoodCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissPop();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((AnimalActivity) activity).sendFood(value);
                dismissPop();
            }
        });
        numberpicker.setMaxValue(12);
        //设置最小值
        numberpicker.setMinValue(1);
        //设置当前值
        numberpicker.setValue(1);
        return viewLayout;
    }


    public View wifiListLayout() {
        View viewLayout = activity.getLayoutInflater().inflate(R.layout.item_wifilist_layout, null);
        LinearLayout lin_cancle = viewLayout.findViewById(R.id.lin_cancle);
        ListView lv_wifi_data = viewLayout.findViewById(R.id.lv_wifi_data);
        lv_wifi_data.setAdapter(new WifiListAdapter(activity, wifilistBeans));
        lv_wifi_data.setOnItemClickListener((adapterView, view, i, l) -> {
                ((BindDevActivity) activity).setWifiName(wifilistBeans.get(i).getSsid());
            dismissPop();
        });
        lin_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissPop();
            }
        });
        return viewLayout;
    }








    /**
     * 关闭pop
     */
    public void dismissPop() {
        if (popWindow != null) {
            ScreenUtils.setBackgroundAlpha(activity, 1.0f);
            popWindow.dismiss();
            popWindow = null;
           if ( openType == WIFI_LIST_ANIMAL
                   ||openType ==SEND_FOOD) {
            }
        }
    }

}
